/**
 * 初始化network_point详情对话框
 */
var NetworkTrainPointsInfoDlg = {
    networkTrainPointsInfoData : {}
};

/**
 * 清除数据
 */
NetworkTrainPointsInfoDlg.clearData = function() {
    this.networkTrainPointsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
NetworkTrainPointsInfoDlg.set = function(key, val) {
    this.networkTrainPointsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
NetworkTrainPointsInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
NetworkTrainPointsInfoDlg.close = function() {
    parent.layer.close(window.parent.NetworkTrainPoints.layerIndex);
}

/**
 * 收集数据
 */
NetworkTrainPointsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('empCode')
    .set('empName')
    .set('netbigIntegral')
    .set('annualCredits')
    .set('annualPeriod')
    .set('createDate')
    .set('laborType')
    .set('personState')
    .set('year')
    ;
};

/**
 * 提交添加
 */
NetworkTrainPointsInfoDlg.addSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }
    if(!$("#year").val()){
        Feng.error("年度不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/networkTrainPoints/add", function(data){
        Feng.success("添加成功!");
        window.parent.NetworkTrainPoints.table.refresh();
        NetworkTrainPointsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.networkTrainPointsInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
NetworkTrainPointsInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    if(!$("#year").val()){
        Feng.error("年度不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/networkTrainPoints/update", function(data){
        Feng.success("修改成功!");
        window.parent.NetworkTrainPoints.table.refresh();
        NetworkTrainPointsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.networkTrainPointsInfoData);
    ajax.start();
};

/**
 * 规则计算
 */
NetworkTrainPointsInfoDlg.ruleCal = function () {
    $("#annualPeriod").blur(function(){

        var annualPeriod = $("#annualPeriod").val();
        $.ajax({
            type: "post",
            url: Feng.ctxPath + "/networkTrainPoints/integralCal",
            dataType: "json",
            data: "hours="+annualPeriod,
            success: function(data) {

                // if(data.data){
                    $("#annualCredits").val(data.data);
                // }
            }
        });

    });
};

/**
 * 上传导入
 */
NetworkTrainPointsInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/networkTrainPoints/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xls|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                layer.close(loadIndex);
                if(res.code == 200){
                    layer.alert("导入成功! <br>" + res.message, {
                        icon: 1,
                        skin: 'layui-layer-molv'
                    });

                }else{
                   
                    if(res.data){
                        layer.open({
                            type: 2,
                            title: '重名数据处理',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['820px', '620px'],
                            content: 'repeat', //iframe的url
                            btn: ['保存','取消'],
                            yes: function(index, layero){
                                var repeatData = window["layui-layer-iframe" + index].NetWorkPointRepeat.getContestDatas();
                               
                                $.ajax({
                                    type: 'POST',
                                    url: Feng.ctxPath + "/networkTrainPoints/adds",
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(repeatData),
                                    success: function (res) {
                                        Feng.success("操作成功!");
                                    }
                                });
                                layer.close(index); //如果设定了yes回调，需进行手工关闭
                            }

                        });
                    }else{
                        layer.alert("导入取消! <br>" + res.message, {
                            icon: 2,
                            skin: 'layui-layer-molv'
                        });
                    }

                }
                window.parent.NetworkTrainPoints.table.refresh();

            }
        });

    });
};

/**
 * 选择员工
 */
NetworkTrainPointsInfoDlg.selectEmp = function (obj) {
    var url = Feng.ctxPath +'/employee/select_list';
    if(obj){
        url = url + "?empName="+$("#empName").val();
    }

    layer.open({
        type: 2,
        area: ['800px', '500px'],
        fixed: false, //不固定
        maxmin: true,
        content: url,
        btn: ['确定'],
        yes: function(index, layero){
            var res = window["layui-layer-iframe" + index].Employee.callbackdata();
            $("#empName").val(res.empName);
            $("#empCode").val(res.empCode);
            $("#laborType").val(res.laborType);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }

    });
};

NetworkTrainPointsInfoDlg.template = function(){
    $("#template").click(function () {
        window.open(Feng.ctxPath + "/static/excel/网络培训积分模板.xlsx","_parent");
    });
};
/**
 * 表单校验
 */
NetworkTrainPointsInfoDlg.formValidator = function () {
    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            empCode: {
                validators: {
                    notEmpty: {
                        message: '员工编号不能为空'
                    }
                }
            },
            annualPeriod: {
                validators: {
                    notEmpty: {
                        message: '年度学时不能为空'
                    },
                    numeric: {
                        message: '年度学时只能为数值'
                    }
                }
            },
            annualCredits: {
                validators: {
                    notEmpty: {
                        message: '年度学分不能为空'
                    },
                    numeric: {
                        message: '年度学分只能为数值'
                    }
                }
            },
            netbigIntegral: {
                validators: {
                    notEmpty: {
                        message: '年度网大积分不能为空'
                    },
                    numeric: {
                        message: '年度网大积分只能为数值'
                    }
                }
            }

        }
    });
};


/**
 * 初始化
 */
$(function() {
    NetworkTrainPointsInfoDlg.formValidator();
    NetworkTrainPointsInfoDlg.upload();
    NetworkTrainPointsInfoDlg.ruleCal();
    NetworkTrainPointsInfoDlg.template();
});
